package Design_patterns;

/**
 * 单一设计模式---懒汉式
 */
public class Test01_Lazy {
    public static void main(String[] args) {
        Lazy newInstance = Lazy.getNewInstance();
        Lazy newInstance1 = Lazy.getNewInstance();
        System.out.println(newInstance == newInstance1);
//        Integer a = 1;
//        Integer b =1;
//        System.out.println(a == b);
//        Integer a1 = 128;
//        Integer b1 = 128;
//        System.out.println(a1 == b1);
        
    }
}

class Lazy {
    //1.构造方法私有化
    private Lazy() {
    }

    //2.声名类对象，没有初始化
    //4.也需要是静态的
    private static Lazy isInstance = null;

    ////3.提公共的静态方法使外界调用
    public static Lazy getNewInstance() {
        //效率不高
      /*  synchronized (Lazy.class) {
            if (isInstance == null) {
                isInstance = new Lazy();
            }
            return isInstance;
        }*/
        if (isInstance == null) {
            synchronized (Lazy.class) {
                if (isInstance == null) {
                    isInstance = new Lazy();
                }
            }
        }
        return isInstance;
    }
}